package com.qiaofang.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.DateUtils;
import com.qiaofang.common.model.UiListBean;
import com.qiaofang.common.response.CommonResponse;
import com.qiaofang.common.utils.StringUtil;
import com.qiaofang.dto.ListApiLogDTO;
import com.qiaofang.dto.ListSmsHistoryDTO;
import com.qiaofang.entity.SmsHistory;
import com.qiaofang.request.ListApiLogRequest;
import com.qiaofang.request.SendApiLogAgainRequest;
import com.qiaofang.request.SmsHistoryRequest;
import com.qiaofang.response.ListApiLogResponse;
import com.qiaofang.response.ListSmsHistoryResponse;
import com.qiaofang.service.ApiLogService;
import com.qiaofang.service.SmsHistoryService;
import com.qiaofang.util.BatchReportVO;
import com.qiaofang.vo.SmsHistoryExportVO;
import com.qiaofang.vo.SmsHistoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = {"短信"})
@RequestMapping(value = "/systemSmsHistory", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
public class SmsHistoryController {

    @Autowired
    private SmsHistoryService smsHistoryService;

    @PostMapping(value = "/listSmsHistorySummary")
    @ApiOperation(value = "短信历史统计")
    public ListSmsHistoryResponse listSmsHistorySummary(@RequestBody SmsHistoryRequest request) throws ParseException {
        ListSmsHistoryResponse response = new ListSmsHistoryResponse();
        ListSmsHistoryDTO dto = new ListSmsHistoryDTO();
        dto.setCompanyName(request.getCompanyName());
        String start = request.getCreateTimeStart();
        String end = request.getCreateTimeEnd();
        if(StringUtils.isNotBlank(start)){
            start += " 00:00:00";
            dto.setCreateTimeStart(DateUtils.parseDate(start));
        }
        if(StringUtil.isNotBlank(end)){
            end += " 23:59:59";
            dto.setCreateTimeEnd(DateUtils.parseDate(end));
        }
        response.setData(smsHistoryService.listSmsHistorySummary(dto));
        return response;
    }

    @GetMapping(value = "exportSmsHistorySummary")
    @ApiOperation(value = "导出短信历史统计")
    public void exportSmsHistorySummary(SmsHistoryRequest request, HttpServletResponse response) throws IOException,ParseException {
        ListSmsHistoryDTO dto = new ListSmsHistoryDTO();
        dto.setCompanyName(request.getCompanyName());
        String start = request.getCreateTimeStart();
        String end = request.getCreateTimeEnd();
        if(StringUtils.isNotBlank(start)){
            start += " 00:00:00";
            dto.setCreateTimeStart(DateUtils.parseDate(start));
        }
        if(StringUtil.isNotBlank(end)){
            end += " 23:59:59";
            dto.setCreateTimeEnd(DateUtils.parseDate(end));
        }
        List<SmsHistoryVO> voList = smsHistoryService.listSmsHistorySummary(dto).getList();
        List<SmsHistoryExportVO> exportVOList = new ArrayList<>();
        if(null != voList && voList.size() > 0){
            for(SmsHistoryVO vo: voList){
                SmsHistoryExportVO exportVO = new SmsHistoryExportVO();
                exportVO.setCompanyName(vo.getCompanyName());
                exportVO.setSendCount(vo.getSendCount());
                exportVOList.add(exportVO);
            }
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=短信统计.xlsx");
        EasyExcel.write(response.getOutputStream(), SmsHistoryExportVO.class).sheet("短信统计").doWrite(exportVOList);
    }

    @PostMapping(value = "/listSmsHistoryFailure")
    @ApiOperation(value = "短信失败记录")
    public ListSmsHistoryResponse listSmsHistoryFailure(@RequestBody SmsHistoryRequest request) throws ParseException {
        ListSmsHistoryResponse response = new ListSmsHistoryResponse();
        ListSmsHistoryDTO dto = new ListSmsHistoryDTO();
        dto.setCompanyName(request.getCompanyName());
        dto.setContent(request.getContent());
        String start = request.getCreateTimeStart();
        String end = request.getCreateTimeEnd();
        if(StringUtils.isNotBlank(start)){
            start += " 00:00:00";
            dto.setCreateTimeStart(DateUtils.parseDate(start));
        }
        if(StringUtil.isNotBlank(end)){
            end += " 23:59:59";
            dto.setCreateTimeEnd(DateUtils.parseDate(end));
        }
        response.setData(smsHistoryService.listSmsHistoryFailure(dto, request.getPage()));
        return response;
    }

    @RequestMapping("/sendFailureSmsAgain")
    @ApiOperation(value = "短信失败补发")
    public CommonResponse sendFailureSmsAgain(@RequestBody SmsHistoryRequest request){
        CommonResponse response = smsHistoryService.sendFailureSmsAgain(Long.valueOf(request.getId()));
        return response;
    }

}
